package com.happyhome.services;

import java.text.NumberFormat;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.happyhome.constants.AppConstant;
import com.happyhome.constants.ComTxManagerName;
import com.happyhome.dao.RoomDao;
import com.happyhome.datamodel.House;
import com.happyhome.datamodel.Image;
import com.happyhome.datamodel.Room;

@Service
public class RoomServiceImpl implements RoomService {

	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(RoomServiceImpl.class);

	@Autowired
	private RoomDao RoomDao;

	@Autowired
	private ImageService imageService;
	
	@Override
	public Room findRoomById(Long id) {
		Room Room = RoomDao.find(id);
		return Room;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public Room merge(Room Room) {
		return RoomDao.merge(Room);
	}

	@Override
	public List<Room> getAll() {
		return RoomDao.findAll();
	}
	
	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public Room findRoomByIdAndGetHouse(Long id) {
		Room room = RoomDao.find(id);
		House house = room.getHouse();
		String codeHouse = house.getCodeHouse();
		house.setCodeHouse(codeHouse);
		room.setHouse(house);
		return room;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public List<Room> getRoomsData(Long limit) {
		List<Room> rooms = RoomDao.getRoomsData(limit);
		if(rooms != null && rooms.size() > 0){
			NumberFormat currencyFormatter = NumberFormat.getInstance();
			for(Room room : rooms){
				Image image = imageService.getImageByAIdIsDisplay(room.getIdRoom(), AppConstant.ROOMS_CODE);
				if(image == null){
					image = imageService.getImageNewestAId(room.getIdRoom(), AppConstant.ROOMS_CODE);
				}
				if(image != null){
					room.setUrl("/" + AppConstant.APP_URL + "/picture/" + image.getId());
				}
				// formate and set price room value
				if(room.getPriceRoom() != null){
					room.setPriceRoomValue(currencyFormatter.format(room.getPriceRoom()) + " $");
				}else{
					room.setPriceRoomValue("0 $");
				}
				if(room.getHouse() != null){
					room.setHomeValue(room.getHouse().getNameHouse());
					room.setAddress(room.getHouse().getAddress());
				}
			}
		}
		return rooms;
	}
	
	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public void setDisplayCavas(List<Long> listRoomsId) {
		List<Room> listNewsInList = RoomDao.getListRoomsInListId(listRoomsId);
		for(Room roomIn : listNewsInList){
			roomIn.setIsDisplay(Boolean.valueOf("true"));
		}
		
		List<Room> listNewsOutList = RoomDao.getListRoomsNotInListId(listRoomsId);
		for(Room roomIn : listNewsOutList){
			roomIn.setIsDisplay(Boolean.valueOf("false"));
		}
		RoomDao.flush();
	}

	@Override
	public List<Room> getListRoomsIsDisplay() {
		return RoomDao.getListRoomsIsDisplay();
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public List<Room> getListRoomsByHouseId(Long idHouse, Integer limit) {
		List<Room> listRooms = RoomDao.getListRoomsByHouseId(idHouse, limit);
		NumberFormat currencyFormatter = NumberFormat.getInstance();
		for(Room tempRoom : listRooms){
			// get image is checked to display on cavas
			Image image = imageService.getImageByAIdIsDisplay(tempRoom.getIdRoom(), AppConstant.ROOMS_CODE);
			if(image == null){
				// get the last newest house
				image = imageService.getImageNewestAId(tempRoom.getIdRoom(), AppConstant.ROOMS_CODE);
			}
			if(image != null){
				tempRoom.setUrl("/picture/"+ image.getId());
			}
			// formate and set price room value
			if(tempRoom.getPriceRoom() != null){
				tempRoom.setPriceRoomValue(currencyFormatter.format(tempRoom.getPriceRoom()) + " $");
			}else{
				tempRoom.setPriceRoomValue("0 $");
			}
			if(tempRoom.getHouse() != null){
				tempRoom.setHomeValue(tempRoom.getHouse().getNameHouse());
				tempRoom.setAddress(tempRoom.getHouse().getAddress());
			}
		}
		return listRooms;
	}

	@Override
	@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
	public List<Room> getListRoomByDistrict(List<Long> idDistricts, Long minCost, Long maxCost, Long limit) {
		List<Room> listRooms = RoomDao.getListRoomByDistrict(idDistricts, minCost, maxCost, limit);
		NumberFormat currencyFormatter = NumberFormat.getInstance();
		for(Room tempRoom : listRooms){
			// get image is checked to display on cavas
			Image image = imageService.getImageByAIdIsDisplay(tempRoom.getIdRoom(), AppConstant.ROOMS_CODE);
			if(image == null){
				// get the last newest house
				image = imageService.getImageNewestAId(tempRoom.getIdRoom(), AppConstant.ROOMS_CODE);
			}
			if(image != null){
				tempRoom.setUrl("/picture/"+ image.getId());
			}
			// formate and set price room value
			if(tempRoom.getPriceRoom() != null){
				tempRoom.setPriceRoomValue(currencyFormatter.format(tempRoom.getPriceRoom()) + " $");
			}else{
				tempRoom.setPriceRoomValue("0 $");
			}
			if(tempRoom.getHouse() != null){
				tempRoom.setHomeValue(tempRoom.getHouse().getNameHouse());
				tempRoom.setAddress(tempRoom.getHouse().getAddress());
			}
		}
		return listRooms;
	}
}
